package com.hbsoft.sub;

import java.awt.BorderLayout;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.io.IOException;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import com.ant4j.popularize.ui.MainFrame;
import com.hbsoft.com.ExportExcel;

public class MyAcctionClass {
	
	private MainFrame mf = null;

	private MyToolPanel toolpanel = null;

	private ContentPanel contentpanel = null;

	private BillSelectPanel bsp;

	private FillInBillPanel fibp;

	private DeleteRegisterSelectPanel drsp;

	private AccountManagePanel amp;
	
	private GiveBackManagePanel gbmp;
	
	private DefaultTableModel model = null;
	private JTable table = null;
	
	private int state = 0;

	
	public MyAcctionClass(MainFrame mf, MyToolPanel toolpanel,
			ContentPanel contentpanel) {
		super();
		this.mf = mf;
		this.toolpanel = toolpanel;
		this.contentpanel = contentpanel;
		
		fibp = contentpanel.getFibp();
		bsp = contentpanel.getBsp();
		drsp = contentpanel.getDrsp();
		amp = contentpanel.getAmp();
		gbmp = contentpanel.getGbmp();
		
		table = bsp.getTable();
		
		initActionLister();

	}

	private void check(int n)
	{
		contentpanel.removeAll();
		
		bsp.setVisible(false);
		fibp.setVisible(false);
		drsp.setVisible(false);
		amp.setVisible(false);
		gbmp.setVisible(false);
		switch(n)
		{
			case 1:contentpanel.add(bsp,BorderLayout.CENTER);bsp.setVisible(true);break;
			case 2:contentpanel.add(fibp,BorderLayout.CENTER);fibp.setVisible(true);break;
			case 3:contentpanel.add(drsp,BorderLayout.CENTER);drsp.setVisible(true);break;
			case 4:contentpanel.add(gbmp,BorderLayout.CENTER);gbmp.setVisible(true);break;
			case 5:contentpanel.add(amp,BorderLayout.CENTER);amp.setVisible(true);break;
			default:break;
		}
	}
	
	private void initActionLister() {
		mf.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				System.exit(0);
			}
		});
		
		mf.addWindowStateListener(new WindowStateListener(){
			public void windowStateChanged(WindowEvent arg0) {
				mf.repaint();
			}
		});

		toolpanel.getBillselectButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				check(1);
			}
		});

		toolpanel.getInsertAndPrint().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				check(2);
			}
		});

		toolpanel.getSelectDeletedRegister().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				check(3);
			}
		});
		
		toolpanel.getGiveBackManage().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				check(4);
			}
		});
		
		toolpanel.getAccountManage().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				check(5);
			}
		});

		toolpanel.getCompter().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				Runtime rt = Runtime.getRuntime();
				try {
					rt.exec("calc");
				} catch (IOException ex) {
					ex.printStackTrace();
				}

			}
		});
		
		//������excel��ť��Ӽ�����
		bsp.getToExcel().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				ExportExcel exp = new ExportExcel(table);
				exp.export();
			}
		});

		
		//��ӡ��ť��Ӽ�����
		bsp.getPrintButton().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				try 
				{
					table.print();
				} 
				catch (PrinterException e) 
				{
					e.printStackTrace();
				}
			}
		});
	}
}
